package com.renwei.yunlong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.fragment.WorkOrderFragment;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.renwei.yunlong.utils.ToastUtil;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 登录页面
 * <p>
 * Created by Administrator on 2017/8/22.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {


    @Bind(R.id.button_login)
    Button buttonLogin;
    @Bind(R.id.tv_forget_pwd)
    TextView tvForgetPwd;
    @Bind(R.id.tv_register)
    TextView tvRegister;
    @Bind(R.id.tv_service)
    TextView tvService;
    @Bind(R.id.view_service)
    View viewService;
    @Bind(R.id.rl_service)
    RelativeLayout rlService;
    @Bind(R.id.tv_owner)
    TextView tvOwner;
    @Bind(R.id.view_owner)
    View viewOwner;
    @Bind(R.id.rl_owner)
    RelativeLayout rlOwner;
    @Bind(R.id.et_account)
    EditText etAccount;
    @Bind(R.id.et_password)
    EditText etPassword;
    private Intent intent;

    Map<String, String> map = new HashMap<String, String>();

    private String companyTypeStr = "2";//默认是服务商   企业类型

    private ACache mCache;//数据缓存


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        mCache = ACache.get(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        buttonLogin.setOnClickListener(this);
        tvForgetPwd.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
        rlService.setOnClickListener(this);
        rlOwner.setOnClickListener(this);

        TextChange textChange = new TextChange();
        etAccount.addTextChangedListener(textChange);
        etPassword.addTextChangedListener(textChange);

    }

    //服务商登录
    private void serviceLoginHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(LoginActivity.this);
        hp.setUrl(Api.SERVICE_LOGIN_URL);
        hp.setshowDialogcontent("正在登录");
        hp.setRequsetId(1);
        hp.putParam("jsonBean",jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //业主登录
    private void ownerLoginHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(LoginActivity.this);
        hp.setUrl(Api.OWNER_LOGIN_URL);
        hp.setshowDialogcontent("正在登录");
        hp.setRequsetId(2);
        hp.putParam("jsonBean",jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_login:
                String account = etAccount.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                String jsonBean="{userName:\""+account+"\",password:\""+password+"\"}";
                Log.e("登录地址",jsonBean);
                if ("1".equals(companyTypeStr)){
                    ownerLoginHttp(jsonBean);
                }else {
                    serviceLoginHttp(jsonBean);
                }

                break;
            case R.id.tv_forget_pwd://忘记密码
                intent = new Intent(this, PasswordForGetActivity.class);
                intent.putExtra("companyType",companyTypeStr);
                startActivity(intent);
                break;
            case R.id.tv_register:

                intent = new Intent(this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_service:
                tvService.setTextColor(getResources().getColor(R.color.blue_color));
                viewService.setBackgroundColor(getResources().getColor(R.color.blue_color));
                tvOwner.setTextColor(getResources().getColor(R.color.text_dark_gray_color));
                viewOwner.setBackgroundColor(getResources().getColor(R.color.text_dark_gray_color));

                companyTypeStr = "2";

                break;
            case R.id.rl_owner:
                tvOwner.setTextColor(getResources().getColor(R.color.blue_color));
                viewOwner.setBackgroundColor(getResources().getColor(R.color.blue_color));
                tvService.setTextColor(getResources().getColor(R.color.text_dark_gray_color));
                viewService.setBackgroundColor(getResources().getColor(R.color.text_dark_gray_color));

                companyTypeStr = "1";

                break;
            default:
                break;
        }
    }

    /**
     *
     * 网络请求回调
     *
     * @param requestId
     * @param result
     */
    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId){
            case 1://服务商
                Log.e("服务商登录返回",result);

                ServiceLoginBean bean = new Gson().fromJson(result,ServiceLoginBean.class);

                if (bean.getMessage().getCode() == 200){//登录成功
                    mCache.put("serviceLoginBean",bean);
                    mCache.put("companyType","2");
                    intent=new Intent(this,MainActivity.class);
                    startActivity(intent);
                    this.finish();
                }else if (bean.getMessage().getCode() == 1001){//账号或密码为空
                    ToastUtil.showShort(this,"账号或密码为空");
                }else if (bean.getMessage().getCode() == 201){//系统执行异常
                    ToastUtil.showShort(this,"系统执行异常");
                }else if (bean.getMessage().getCode() == 1014){//用户不存在
                    ToastUtil.showShort(this,"用户不存在");
                }else if (bean.getMessage().getCode() == 1015){//用户密码错误
                    ToastUtil.showShort(this,"用户密码错误");
                }else if (bean.getMessage().getCode() == 1016){//用户已被企业移除
                    ToastUtil.showShort(this,"用户已被企业移除");
                }
                break;
            case 2://业主

                Log.e("业主登录返回",result);

                LoginBean loginBean = new Gson().fromJson(result,LoginBean.class);

                if (loginBean.getMessage().getCode() == 200){//登录成功
                    mCache.put("loginBean",loginBean);
                    mCache.put("companyType","1");
                    intent=new Intent(this,MainActivity.class);
                    startActivity(intent);
                    this.finish();

                }else if (loginBean.getMessage().getCode() == 1001){//账号或密码为空
                    ToastUtil.showShort(this,"账号或密码为空");
                }else if (loginBean.getMessage().getCode() == 201){//系统执行异常
                    ToastUtil.showShort(this,"系统执行异常");
                }else if (loginBean.getMessage().getCode() == 1014){//用户不存在
                    ToastUtil.showShort(this,"用户不存在");
                }else if (loginBean.getMessage().getCode() == 1015){//用户密码错误
                    ToastUtil.showShort(this,"用户密码错误");
                }else if (loginBean.getMessage().getCode() == 1016){//用户已被企业移除
                    ToastUtil.showShort(this,"用户已被企业移除");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
    }

    //判断输入框是否为空、空则按钮灰色不可点击、不为空则变蓝色可点击
    private void setBackground(Map<String, String> text) {
        if (!TextUtils.isEmpty(text.get("1"))
                && !TextUtils.isEmpty(text.get("2"))) {
            buttonLogin.setBackgroundResource(R.drawable.shape_register_pre);
        } else {
            buttonLogin.setBackgroundResource(R.drawable.shape_register_nor);
        }
    }

    class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            map.put("1", etAccount.getText().toString().trim());
            map.put("2", etPassword.getText().toString().trim());
            setBackground(map);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
